package demo.java.lang.management.jvm;

import demo.java.lang.management.vo.JVMClassLoading;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;

/**
 * 收集类加载信息
 * @date 16/1/18
 */
public class ClassLoadingCollector {

    private static final Logger logger = LoggerFactory.getLogger(ClassLoadingCollector.class);

    private ClassLoadingMXBean ClassLoading;
    private CompilationMXBean Compilation;

    public ClassLoadingCollector(ClassLoadingMXBean ClassLoading, CompilationMXBean Compilation) {
        this.ClassLoading = ClassLoading;
        this.Compilation = Compilation;
    }


    public JVMClassLoading collect() {
        JVMClassLoading classLoadingInfo = new JVMClassLoading();
        try {
            classLoadingInfo.setLoadedClassCount(ClassLoading.getLoadedClassCount());
            classLoadingInfo.setTotalCompilationTime(Compilation.getTotalCompilationTime());
            classLoadingInfo.setTotalLoadedClassCount(ClassLoading.getTotalLoadedClassCount());
            classLoadingInfo.setUnloadedClassCount(ClassLoading.getUnloadedClassCount());
        }catch (Throwable e){
            logger.error("ClassLoadingCollector collect error, {}", e.getMessage());
            return null;
        }

        return classLoadingInfo;
    }

}
